﻿using System.Text;

namespace CommandLib
{
    /// <summary>
    /// 自定义随机生成器
    /// </summary>
    public class RandomHelper
    {
        private string[] BaseString = {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y", "z"  };
        private static Random rd = new Random();
        /// <summary>
        /// 获取除机值
        /// </summary>
        public string GetValue { get; set; }
          /// <summary>
        /// 随机生成数字
        /// </summary>
        /// <param name="max">最大值</param>
        /// <param name="lenth">生成长度</param>
        public RandomHelper(int max, int lenth)
        {
            GetValue = rd.Next(0, max).ToString().PadLeft(lenth);
        }
        /// <summary>
        /// 随机生面数字
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <param name="lenth">生成长度</param>
        public RandomHelper(int min,int max,int lenth) {

            GetValue = rd.Next(min,max).ToString().PadLeft(lenth);
        }
        /// <summary>
        /// 自定义生成器
        /// </summary>
        /// <param name="lenth">生成位数</param>
        public RandomHelper(int lenth)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < lenth; i++)
            {
                var bit = rd.Next(0, BaseString.Length);
                sb.Append(BaseString[bit]);
            }
            GetValue = sb.ToString();

        }
        /// <summary>
        /// 自定义生成器
        /// </summary>
        /// <param name="bs">生成基数</param>
        /// <param name="lenth">生成随机码长度</param>
        public RandomHelper(string[] bs, int lenth)
        {
            if (bs != null && bs.Length > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < lenth; i++)
                {
                    var bit = rd.Next(0, bs.Length);
                    sb.Append(bs[bit]);
                }
                GetValue = sb.ToString();
            }

        }
    }
}
